/***************************************************************************
 *   Copyright (C) 2008 by Eduardo Gurgel Pinho                            *
 *   edgurgel@gmail.com                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/

#ifndef __SCENE_H__
#define __SCENE_H__

#include "color.h"
#include "object/object.h"
#include "object/sphere.h"
#include "object/plane.h"
#include "object/cone.h"
#include "object/cylinder.h"
#include "object/disc.h"
#include "object/triangle.h"
#include "object/quadrilateral.h"
#include "object/mesh.h"
#include "light/light.h"
#include "light/punctual_light.h"
#include "light/square_light.h"
#include "math/ray.h"
#include "math/vector.h"

#include <boost/serialization/nvp.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/multi_array.hpp>

#include <vector>

using namespace boost::tuples;

namespace Sirat {

namespace Common {
	
	/**
	 * Scene class.
	 * Bunch of objects and lights.
	 */
	
class Scene {
public:

	/**
	 * Simple constructor.
	 * @param _color Background color.
	 * @param _ambient Old value.
	 */
	Scene(const Color& _background,const double _ambient);
	
	/**
	 * Default constructor.
	 * Boost Serialization needs a default constructor
	 * @see Scene(const Scene&)
	 */
	Scene();
	
	/**
	 * Copy constructor.
	 * @param _scene
	 */
	Scene(const Scene& _scene);
	
	/**
	 * Destructor
	 */
	~Scene();

	/**
	 * Main function.
	 * @param _ray 
	 * @return Nearest Object and respectively point.
	 */
	tuple<Object::Object*,Math::Vector> nearest_object(const Math::Ray& _ray);
	
	/**
	 * Test if any object is on this direction.
	 * @param _ray
	 * @param _t
	 * @return True if we have occlusion.
	 */
	bool object_occlusion(const Math::Ray& _ray,const double _t);
	
	void add_objectlist(std::vector<Object::Object*>*);
	void add_object(Object::Object*);
	void add_lightlist(std::vector<Light::Light*>*);
	void add_light(Light::Light*);
	
	void remove_object(const unsigned int);
	void remove_light(const unsigned int);
	
	void set_background(const Color&);
	void set_attenuation(bool);
	void set_attenuation(double= 0.0,double = 0.0,double = 0.0);
	void set_ambient(double);
	void set_optimization(bool _enable = false, unsigned int _divisions = 40);
	
	unsigned int get_objects_size() const;
			
	unsigned int get_lights_size() const;
	
	/**
	 * @param  _index
	 * @return Object with given index.
	 */
	Object::Object* get_object(const unsigned int _index) const;
	
	/**
	 * @param  _index
	 * @return Light with given index.
	 */
	Light::Light* get_light(const unsigned int _index) const;
	
	Color get_background() const;
	double get_attenuation(const double) const;
	double get_attenuation_value(const unsigned short) const;
	double get_ambient() const;
	
	Scene& operator=(const Scene&);

private:
	typedef boost::multi_array<std::vector<Object::Object*>, 3> Objects_Grid;

	friend class boost::serialization::access;
	template<class Archive> void serialize(Archive&, const unsigned int);

	void update_grid();
	tuple<double, Math::Vector, Object::Object*> intersect_grid(const Math::Ray& _ray);
	bool inside_grid(unsigned int _x, unsigned int _y, unsigned int _z);
	
	std::vector<Object::Object*> objects;
	std::vector<Object::Object*> limitable_objects;
	std::vector<Light::Light*> lights;
	Color background;
	double ambient;
	bool attenuation;
	double a,b,c; // Attenuation distance function: ax2 + bx + c 

	bool optimization;
	unsigned int optimization_divisions;
	Objects_Grid* grid;
	Math::Vector grid_min_point, grid_max_point;
};

template<class Archive> void Scene::serialize(Archive& ar, const unsigned int /* file_version */){
	// Registering Object casting 
	ar.register_type(static_cast<Object::Plane*>(0));
	ar.register_type(static_cast<Object::Cone*>(0));
	ar.register_type(static_cast<Object::Disc*>(0));
	ar.register_type(static_cast<Object::Quadrilateral*>(0));
	ar.register_type(static_cast<Object::Triangle*>(0));
	ar.register_type(static_cast<Object::Sphere*>(0));
	ar.register_type(static_cast<Object::Cylinder*>(0));
	ar.register_type(static_cast<Object::Mesh*>(0));
	ar.register_type(static_cast<Light::Punctual_Light*>(0));
	ar.register_type(static_cast<Light::Square_Light*>(0));
	ar	& BOOST_SERIALIZATION_NVP(objects)
		& BOOST_SERIALIZATION_NVP(limitable_objects)
		& BOOST_SERIALIZATION_NVP(lights)
		& BOOST_SERIALIZATION_NVP(background)
		& BOOST_SERIALIZATION_NVP(ambient)
		& BOOST_SERIALIZATION_NVP(attenuation)
		& BOOST_SERIALIZATION_NVP(a)
		& BOOST_SERIALIZATION_NVP(b)
		& BOOST_SERIALIZATION_NVP(c)
		& BOOST_SERIALIZATION_NVP(optimization)
		& BOOST_SERIALIZATION_NVP(optimization_divisions);
	
	if(optimization) update_grid();
}

inline
bool Scene::object_occlusion(const Math::Ray& _ray,const double _t) {
	Math::Vector origin = _ray.get_origin();
	tuple<Object::Object*,Math::Vector> intersection = nearest_object(_ray);
	if(intersection.get<0>() == 0) return false;
	else if(intersection.get<1>().distance(origin) < _t) return true;

	return false;
}

}

}

#endif
